﻿using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WroxBlog.Controllers;
using WroxBlog.Models;
using WroxBlog.Models.Interfaces;

namespace WroxBlog.Tests
{
    [TestClass()]
    public class CategoryControllerTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private MockRepository mocks;

        private IPostRepository postMockContext;
        private ICategoryRepository categoryMockContext;

        [TestInitialize]
        public void SetupMocks()
        {
            mocks = new MockRepository();
            postMockContext = mocks.DynamicMock<IPostRepository>();
            categoryMockContext = mocks.DynamicMock<ICategoryRepository>();
        }


        [TestMethod()]
        public void CategoryIndexTest()
        {
            string categoryName = "MVC";

            CategoryCollection fakeCategories = new CategoryCollection();
            for (int i = 0; i < 5; i++)
            {
                fakeCategories.Add(new Category()
                {
                    ID = i
                });
            }

            PostCollection fakePosts = new PostCollection();
            for (int i = 0; i < 10; i++)
            {
                fakePosts.Add(new Post()
                {
                    ID = i
                });
            }

            using (mocks.Record())
            {
                Expect.Call(categoryMockContext.GetCategories()).Return(fakeCategories);
                Expect.Call(postMockContext.GetPostsInCategory(categoryName)).Return(fakePosts);
            }

            CategoryController controller = new CategoryController
                (postMockContext, categoryMockContext);

            using (mocks.Playback())
            {
                ActionResult result = controller.CategoryIndex(categoryName);
                ViewResult viewResult = result as ViewResult;

                string title = (string)viewResult.ViewData["Title"];
                Assert.AreEqual(categoryName, title);

                CategoryCollection categories = viewResult.ViewData["Categories"]
                    as CategoryCollection;
                Assert.AreEqual(5, categories.Count);

                PostCollection posts = viewResult.ViewData["Posts"]
                    as PostCollection;
                Assert.AreEqual(10, posts.Count);
            }
        }
    }
}
